package eg.com.itiga.business.utils;


import java.util.Properties;
import java.util.ResourceBundle;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;


public class Mail 
{
	//String[] recievers;
	String reciever;
	String message;	
	String subject;
	String senderEmail;
	String senderPassword;
	
	private String SMTP_HOST_NAME;
	private static final String SMTP_PORT = "465";
	private static final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
	
	public Mail(){		
		ResourceBundle props = ResourceBundle.getBundle("props");
		message = props.getString("acceptTemplate");
        senderEmail=props.getString("senderEmail");
        senderPassword=props.getString("emailPassword");        
        System.out.println(senderEmail);                  
	}
	
	public void setReciever(String reciever) {
		this.reciever = reciever;
	}

	public void setMsg(String msg) {
		//this.message = msg;
		
	}

	public void setSubject(String subject) {
		this.subject = subject;
	}

	public void setSenderEmail(String senderEmail) {
		this.senderEmail = senderEmail;
	}

	public void setSenderPassword(String senderPassword) {
		this.senderPassword = senderPassword;
	}

	/*public void setSMTP_HOST_NAME(String sMTPHOSTNAME) {
		SMTP_HOST_NAME = sMTPHOSTNAME;
	}*/

	public void replaceWord(String keyword,String value){
		
		//System.out.println("KeyWord :"+keyword);
		//System.out.println("Value :"+value);
		//System.out.println("Template before :"+message);
        Pattern pattern = Pattern.compile(keyword);
        Matcher matcher = pattern.matcher(message);        
        message = matcher.replaceAll(value);
        //message = matcher.replaceFirst(value);
        //System.out.println("Template = " + message);        
	}
	
	private void setSMTP()
	{
		String lower = senderEmail.toLowerCase();
		
		if(lower.lastIndexOf("yahoo")!=-1)
			SMTP_HOST_NAME="smtp.mail.yahoo.com";
		
		else if(lower.lastIndexOf("gmail")!=-1)
			SMTP_HOST_NAME="smtp.gmail.com";
	
		else if(lower.lastIndexOf("hotmail")!=-1)
			SMTP_HOST_NAME="smtp.live.com";
	
		else if(lower.lastIndexOf("live")!=-1)
			SMTP_HOST_NAME="smtp.live.com";		
	}
	
	public void send() throws MessagingException
	{
		setSMTP();
		
		boolean debug = true;

		Properties props = new Properties();
		props.put("mail.smtp.host", SMTP_HOST_NAME);
		props.put("mail.smtp.auth", "true");
		props.put("mail.debug", "true");
		props.put("mail.smtp.port", SMTP_PORT);
		props.put("mail.smtp.socketFactory.port", SMTP_PORT);
		props.put("mail.smtp.socketFactory.class", SSL_FACTORY);
		props.put("mail.smtp.socketFactory.fallback", "false");

		Session session = Session.getDefaultInstance(props,
		new javax.mail.Authenticator() 
		{

			protected PasswordAuthentication getPasswordAuthentication() 
			{	
				return new PasswordAuthentication(senderEmail, senderPassword);
			}
		});
	   
	    // create a message
	    Message msg = new MimeMessage(session);

	    // set the from and to address
	    InternetAddress addressFrom = new InternetAddress(senderEmail);
	    msg.setFrom(addressFrom);
	    	    
	    InternetAddress addressTo = new InternetAddress(reciever);
	    msg.setRecipient(Message.RecipientType.TO, addressTo);
	    
	    
	    /*
	    InternetAddress[] addressTo = new InternetAddress[recievers.length]; 
	    for (int i = 0; i < recievers.length; i++)
	    {
	        addressTo[i] = new InternetAddress(recievers[i]);
	    }	   
	    msg.setRecipients(Message.RecipientType.TO, addressTo);
	    */
	    
	    //You can also set your custom headers in the Email if you Want
	    msg.addHeader("MyHeaderName", "myHeaderValue");

	    // Setting the Subject and Content Type
	    msg.setSubject(subject);
	    //msg.setContent(message, "text/plain");
	    msg.setContent(message, "text/html");
	   
	    Transport.send(msg);

	}
}
